// pages/routeDetail/index.js
var util = require('../../utils/util.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowActive: '',
    current: 0,
    routeInfo: '',
    id: '',
    userInfo: '',
    line_item: '',
    notice: '',
    userComment: [],
    trip_content: [],
    tabTag: 0,
    isShare: false,
    shareNum: '',
    isPoster: false,
    posters: '',
    recom_uid: '',
    circleId: ''

  },
  back: function () {
    if (this.data.recom_uid != '') {
      wx.reLaunch({
          url: '/pages/index/index'
        })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  toPay: function(e) {
    if (this.data.userInfo === '') {
      wx.showToast({
        title: '未登录,跳转登录中...',
        icon: 'none',
        duration: 2000
      })
      setTimeout(item => {
        wx.reLaunch({
          url: '/pages/index/index?id=4'
        })
      }, 1000)

      return false;
    }
    if (this.data.nowActive === '') {
      wx.showModal({
        content: '未选择出发日期',
        showCancel: false
      })
      this.goTop();
      return false;
    }
    if (e.currentTarget.dataset.state == '立即报名') {
      let data = {
        user_id: this.data.userInfo.user_id,
        line_id: this.data.id,
        line_time_id: this.data.nowActive
      }
      if (this.data.circleId !== '') {
        data.recom_uid = this.data.circleId
      } else {
        var recom_uid = wx.getStorageSync('recomUid')
        if (recom_uid) {
          data.recom_uid = recom_uid
        }
      }

      wx.setStorage({
        key: 'orderInfo',
        data: data
      })
      wx.navigateTo({
        url: '/pages/firmOrder/index',
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.userInfo = util.isLogin();
    this.data.id = options.id;
    this.init(options.id);
    if (options.recom_uid) {
      this.data.recom_uid = options.recom_uid
      wx.setStorage({
        key: 'recomUid',
        data: options.recom_uid
      })
    }
    if (options.circleId) {
      this.data.circleId = options.circleId;
      console.log(options.circleId)
    }
    if (this.data.userInfo !== '') {
      this.lineEvaluate(options.id);
    }
  },
  init(id) {
    let that = this;
    util.http({
      url: 'api/miniwechat/line/index',
      data: {
        line_id: id
      }
    }).then((data) => {
      console.log(data)
      if (data.line_time_list.length === 1) {
        this.setData({
          nowActive: data.line_time_list[0].line_time_id
        })
      }
      
      switch (data.line_status) {
        case 0:
          data.line_status = '取消';
          break;
        case 1:
          data.line_status = '立即报名';
          break;
        case 2:
          data.line_status = '报名结束';
          break;
        case 3:
          data.line_status = '已下架';
          break;
      }
      if (data.line_trip.length) {
        data.line_trip.forEach((item,index)=>{
          WxParse.wxParse('trip' + index, 'html', item.trip_content, that);
          item.content = this.data['trip' + index];
        })
        // let listRes = data.line_trip; //要解析的数据
        // for (let i = 0; i < listRes.length; i++) {
        //   WxParse.wxParse('topic' + i, 'html', listRes[i].trip_content, that);
        //   if (i === listRes.length - 1) {
        //     WxParse.wxParseTemArray("listArr", 'topic', listRes.length, that)
        //   }
        // }
        // let list = this.data.listArr;
        // for (let i = 0; i < listRes.length; i++) {
        //   list[i]['a'] = listRes[i]['title'];
        // }
        // list.map((item, index, arr) => {
        //   arr[index][0].day = listRes[index]['day'];
        //   arr[index][0].trip_text = listRes[index]['trip_text'];
        //   arr[index][0].breakfast = listRes[index]['breakfast'];
        //   arr[index][0].lunch = listRes[index]['lunch'];
        //   arr[index][0].dinner = listRes[index]['dinner'];
        //   arr[index][0].hotel = listRes[index]['hotel'];
        // });
        // this.setData({
        //   list: list
        // })
      }
      WxParse.wxParse('lineItem', 'html', data.line_item, that, 15)
      WxParse.wxParse('notice', 'html', data.notice, that, 15)
      this.setData({
        routeInfo: data,
      })
      if (this.data.userInfo !== '' && data.line_time_list.length > 0) {
        util.http({
          url: 'api/miniwechat/index/share',
          data: {
            user_id: this.data.userInfo.user_id,
            line_id: id,
            line_time_id: data.line_time_list.length > 0 ? data.line_time_list[0].line_time_id : '',
            
          }
        }).then((data) => {
          this.setData({
            shareNum: data
          })
        }).catch((err) => {
          wx.showToast({
            title: String(err),
            icon: 'none',
            duration: 2000
          })
        })
      }

    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })

  },
  dateChange(e) {
    this.data.current = e.detail.current;
  },
  goExpertDetail(e) {
    wx.navigateTo({
      url: '/pages/expertDetail/index?id=' + e.currentTarget.dataset.id,
    })
  },
  prevChange(e) {
    if (this.data.routeInfo.line_time_list.length > 3) {
      this.setData({
        current: ((this.data.current === 0) ? 0 : this.data.current - 1)
      })
    }

  },
  nextChange(e) {
    if (this.data.routeInfo.line_time_list.length > 3) {
      this.setData({
        current: ((this.data.current === (this.data.routeInfo.line_time_list.length - 3)) ? (this.data.routeInfo.line_time_list.length - 3) : this.data.current + 1)
      })
    }
  },
  dateChange(e) {
    this.data.current = e.detail.current;
  },
  lineEvaluate(id) {
    let that = this;
    util.http({
      url: '/api/miniwechat/line/evaluate',
      data: {
        line_id: id
      }
    }).then((data) => {
      console.log("lineEvaluate evaluate result------↓↓↓↓")
      console.log(data);
      this.setData({
        userComment: data
      })
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  goExpertDetail(e) {
    wx.navigateTo({
      url: '/pages/expertDetail/index?id=' + e.currentTarget.dataset.id,
    })
  },
  prevChange(e) {
    this.setData({
      current: ((this.data.current === 0) ? 0 : this.data.current - 1)
    })
  },
  nextChange(e) {
    this.setData({
      current: ((this.data.current === (this.data.routeInfo.line_time_list.length - 3)) ? (this.data.routeInfo.line_time_list.length - 3) : this.data.current + 1)
    })
  },
  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.routeInfo.phone
    })
  },
  collect(index) {
    let that = this;
    util.http({
      url: 'api/miniwechat/index/coll',
      method: 'POST',
      data: {
        line_id: that.data.id,
        user_id: that.data.userInfo.user_id,
        type: (that.data.routeInfo.is_coll == 0) ? 1 : 0
      }
    }).then((data) => {
      that.data.routeInfo.is_coll = (this.data.routeInfo.is_coll == 0) ? 1 : 0
      that.setData({
        routeInfo: that.data.routeInfo
      })
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  goExpertDetail(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/expertDetail/index?id=' + e.currentTarget.dataset.id,
    })
  },
  goCommentList(e) {
    wx.navigateTo({
      url: '/pages/commentList/index?id=' + this.data.id,
    })
  },
  choseDate(e) {
    this.setData({
      nowActive: e.currentTarget.dataset.id
    })
  },
  goTop(e) { // 一键回到顶部
    wx.pageScrollTo({
      scrollTop: 250
    })
  },
  makePhoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.routeInfo.phone
    })
  },
  collect(index) {
    let that = this;
    util.http({
      url: 'api/miniwechat/index/coll',
      method: 'POST',
      data: {
        line_id: that.data.id,
        user_id: that.data.userInfo.user_id,
        type: (that.data.routeInfo.is_coll == 0) ? 1 : 0
      }
    }).then((data) => {
      that.data.routeInfo.is_coll = (this.data.routeInfo.is_coll == 0) ? 1 : 0
      that.setData({
        routeInfo: that.data.routeInfo
      })
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  tabChange(e) {
    this.setData({
      tabTag: e.currentTarget.dataset.index
    })
  },
  shareShow(e) {
    if (this.data.userInfo === '') {
      wx.showToast({
        title: '请登录后操作',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.setData({
      isShare: true
    })
  },
  shareHide(e) {
    this.setData({
      isShare: false
    })
  },
  onShareAppMessage: function(res) {
    return {
      title: this.data.routeInfo.title,
      imageUrl: this.data.routeInfo.line_cover_image[0],
      path: 'pages/routeDetail/index?id=' + this.data.id + '&recom_uid=' + this.data.userInfo.user_id
    }
  },
  posterShow(e) {
    this.setData({
      isPoster: true,
      isShare: false
    })
    this.getPosters();
  },
  posterHide(e) {
    this.setData({
      isPoster: false
    })
  },
  getPosters() {
    util.http({
      url: 'api/miniwechat/index/qrcode',
      method: 'POST',
      data: {
        line_id: this.data.id,
        user_id: this.data.userInfo.user_id,
        line_time_id: this.data.routeInfo.line_time_list.length > 0 ? this.data.routeInfo.line_time_list[0].line_time_id : '',
      }
    }).then((data) => {
      this.setData({
        posters: data
      })
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  saveImg(e) {
    let that = this;
    wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success() {
        wx.downloadFile({
          url: that.data.posters,
          success(res) {
            if (res.statusCode === 200) {
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  wx.showToast({
                    title: '保存成功',
                    icon: 'none',
                    duration: 2000
                  })
                  that.posterHide();
                },
                fail(err) {
                  wx.showToast({
                    title: '保存失败',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            }
          }
        })
      },
      fail() {
        wx.showToast({
          title: '未获取授权',
          icon: 'none',
          duration: 2000
        })
      }
    })

  }
})